//
//  ParkTableViewController.m
//  SXH
//
//  Created by Jack on 16/1/28.
//  Copyright © 2016年 lyss. All rights reserved.
//

#import "ParkTableViewController.h"
#import "ParkTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "SpotClusterItem.h"
#import "IntroduceViewController.h"
#import "WrapperViewController.h"
#import "LocationManagement.h"
#import "SXH-swift.h"

@interface ParkTableViewController ()
@property (nonatomic, strong) IBOutlet UITableView *tabelViewParks;
@property(nonatomic, strong) RatingBar *myRating;

@end


@implementation ParkTableViewController
@synthesize parksArray;

- (void)viewDidLoad {
    [super viewDidLoad];
    [_tabelViewParks reloadData];
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [parksArray count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *couponTableViewCellIdentifier=@"ParkTableViewCell";
    ParkTableViewCell *cell = (ParkTableViewCell *)[tableView dequeueReusableCellWithIdentifier:couponTableViewCellIdentifier];
    if (cell == nil) {
        NSArray *array = [[NSBundle mainBundle] loadNibNamed:@"ParkTableViewCell" owner:self options:nil];
        cell = [array objectAtIndex:0];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    SpotClusterItem *_parkTemp = [parksArray objectAtIndex:indexPath.row];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:_parkTemp.album_thumb]];
    cell.title.text = _parkTemp.name;
    cell.level.text = [self exchangeLevel:_parkTemp.level];
    cell.distance.text = [self getDistance:_parkTemp.coor];
    self.myRating = [[RatingBar alloc]initWithFrame:CGRectMake(0, 0, 90, 20)];
    self.myRating.rating = [_parkTemp.star doubleValue];
    [cell.rating_s_img_url addSubview:self.myRating];
    
    return cell;
}

- (NSString *)exchangeLevel:(NSString*)level{
    switch ([level intValue]) {
        case 0:
            return @"无";
            break;
        case 1:
            return @"A";
            break;
        case 2:
            return @"AA";
            break;
        case 3:
            return @"AAA";
            break;
        case 4:
            return @"AAAA";
            break;
        case 5:
            return @"AAAAA";
            break;
        default:
            return @"AAAAA";
            break;
    }
}

- (NSString *)getDistance:(CLLocationCoordinate2D)coor{
    LocationManagement *lm = [LocationManagement sharedInstance];
    BMKMapPoint point1 = BMKMapPointForCoordinate(lm.location.coordinate);
    BMKMapPoint point2 = BMKMapPointForCoordinate(coor);
    CLLocationDistance distance = BMKMetersBetweenMapPoints(point1,point2);
    return [NSString stringWithFormat:@"%.3f%@", distance/1000,@"千米"];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 100;
}

#pragma mark -
#pragma mark Table view delegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //当离开某行时，让某行的选中状态消失
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    SpotClusterItem *_parkTemp = [parksArray objectAtIndex:indexPath.row];
    ParkEntity *parkEntity = [ParkEntity new];
    parkEntity.ID = _parkTemp.parkID;
    parkEntity.is_business = _parkTemp.is_business;
    if ([parkEntity.is_business isEqualToString:@"0"] == YES) {
        IntroduceViewController *vc = [[IntroduceViewController alloc] init];
        vc.parkId = [NSString stringWithFormat:@"%@",parkEntity.ID];
        [self.navigationController pushViewController:vc animated:YES];
    } else {
        WrapperViewController *vc = [[WrapperViewController alloc] init];
        vc.parkEntity = parkEntity;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [MobClick beginLogPageView:@"ParkTableViewController"];
}
- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [MobClick endLogPageView:@"ParkTableViewController"];
}

@end
